<?php

namespace App\Http\Service;

use App\Domain\Lendhist\Model\Lendhist;
use App\Util\Result;
use App\Model\Book;
use Illuminate\Support\Facades\DB;


trait ServiceLtBook{

    /**
     * czs
     * @return mixed
     * 景区列表
     */
    public static function getList($keyWord,$limit){
        $model = DB::table('Items as a');
        if (!empty($keyWord)) {
            $model->where("a.name","like", '%'.$keyWord.'%');
        }
        $list = $model
            ->leftJoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->orderBy('a.SerialNo','desc')
            ->select('a.BarCode','b.Title','b.Author','b.Publisher','b.Pages')
            ->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     * czs
     */
    public static function getLists($keyWord,$limit){
        $model = DB::table('ttindex7 as a');
        if (!empty($keyWord)) {
            $model->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('a.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('c.BarCode',"like", '%'.$keyWord.'%')->orwhere("a.Title","like", '%'.$keyWord.'%');
            });
        }
        $list = $model
            ->leftjoin('circ_marc as b','b.IID','=','a.ID')
            ->leftjoin('Items as c','c.MarcID','=','b.MarcID')
            ->where('c.LTStatus',4)
            ->groupBy('a.ID')
            ->select(DB::raw('a.Content as isbn,b.ImageUrl,a.Title,b.ReqNo,a.ID,b.Title as cTitle,a.ImageUrl as img,count(c.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('Items as a')->where('a.LTStatus',4)
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->leftjoin('ttindex7 as c','c.ID','=','b.IID');
        if (!empty($keyWord)) {
            $model2->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('c.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('a.BarCode',"like", '%'.$keyWord.'%')->orwhere("c.Title","like", '%'.$keyWord.'%');
            });
        }
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     * czs
     */
    public static function getSjLists($keyWord,$limit){
        $model = DB::table('ttindex7 as a');
        if (!empty($keyWord)) {
            $model->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('a.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('c.BarCode',"like", '%'.$keyWord.'%')->orwhere("a.Title","like", '%'.$keyWord.'%');
            });
        }
        $list = $model
            ->leftjoin('circ_marc as b','b.IID','=','a.ID')
            ->leftjoin('Items as c','c.MarcID','=','b.MarcID')
            ->where('c.LTStatus',3)
            ->groupBy('a.ID')
            ->select(DB::raw('a.Content as isbn,b.ImageUrl,a.Title,b.ReqNo,a.ID,b.Title as cTitle,a.ImageUrl as img,count(c.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('Items as a')->where('a.LTStatus',3)
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->leftjoin('ttindex7 as c','c.ID','=','b.IID');
        if (!empty($keyWord)) {
            $model2->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('c.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('a.BarCode',"like", '%'.$keyWord.'%')->orwhere("c.Title","like", '%'.$keyWord.'%');
            });
        }
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     * czs
     */
    public static function getXjLists($keyWord,$limit){
        $model = DB::table('ttindex7 as a');
        if (!empty($keyWord)) {
            $model->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('a.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('c.BarCode',"like", '%'.$keyWord.'%')->orwhere("a.Title","like", '%'.$keyWord.'%');
            });
        }
        $list = $model
            ->leftjoin('circ_marc as b','b.IID','=','a.ID')
            ->leftjoin('Items as c','c.MarcID','=','b.MarcID')
            ->where('c.LTStatus',5)
            ->groupBy('a.ID')
            ->select(DB::raw('a.Content as isbn,b.ImageUrl,a.Title,b.ReqNo,a.ID,b.Title as cTitle,a.ImageUrl as img,count(c.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('Items as a')->where('a.LTStatus',5)
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->leftjoin('ttindex7 as c','c.ID','=','b.IID');
        if (!empty($keyWord)) {
            $model2->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('c.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('a.BarCode',"like", '%'.$keyWord.'%')->orwhere("c.Title","like", '%'.$keyWord.'%');
            });
        }
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     * ycBook
     */
    public static function getYcLists($keyWord,$limit){
        //先取异常书
        $model=DB::table('errbook as e')
            ->leftjoin('TagInfo as t','t.rUID','=','e.BookUID')
            ->leftjoin('Items as c','c.BarCode','=','t.Barcode')
            ->leftjoin('circ_marc as b','b.MarcID','=','c.MarcID')
            ->leftjoin('ttindex7 as a','b.IID','=','a.ID');

        if (!empty($keyWord)) {
            $model->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('a.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('c.BarCode',"like", '%'.$keyWord.'%')->orwhere("a.Title","like", '%'.$keyWord.'%');
            });
        }
        $list = $model
            ->select('a.Content as isbn','b.ImageUrl','a.Title','b.ReqNo','a.ID','b.Title as cTitle','a.ImageUrl as img','c.SerialNo','t.Barcode','e.BookUID');
        $res['count'] = count($list->get());
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     * czs
     */
    public static function getWsLists($keyWord,$limit){
        $model = DB::table('ttindex7 as a');
        if (!empty($keyWord)) {
            $model->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('a.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('c.BarCode',"like", '%'.$keyWord.'%')->orwhere("a.Title","like", '%'.$keyWord.'%');
            });
        }
        $list = $model
            ->leftjoin('circ_marc as b','b.IID','=','a.ID')
            ->leftjoin('Items as c','c.MarcID','=','b.MarcID')
            ->where('c.LTStatus',6)
            ->groupBy('a.ID')
            ->select(DB::raw('a.Content as isbn,b.ImageUrl,a.Title,b.ReqNo,a.ID,b.Title as cTitle,a.ImageUrl as img ,count(c.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('Items as a')->where('a.LTStatus',6)
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->leftjoin('ttindex7 as c','c.ID','=','b.IID');
        if (!empty($keyWord)) {
            $model2->where(function ($query) use ($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')->orwhere('c.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('a.BarCode',"like", '%'.$keyWord.'%')->orwhere("c.Title","like", '%'.$keyWord.'%');
            });
        }
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
 */
    public static function getSecCata($id,$limit){
        $model = DB::table('circ_marc as a');
        $list = $model
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',4)
            ->where('a.IID',$id)
            ->groupBy('a.MarcID')
            ->select(DB::raw('a.Price,a.ImageUrl,a.Title,a.ReqNo,a.MarcID,count(b.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('circ_marc as a')
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',4)
            ->where('a.IID',$id);
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    public static function getSecSj($id,$limit){
        $model = DB::table('circ_marc as a');
        $list = $model
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',3)
            ->where('a.IID',$id)
            ->groupBy('a.MarcID')
            ->select(DB::raw('a.Price,a.ImageUrl,a.Title,a.ReqNo,a.MarcID,count(b.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('circ_marc as a')
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',3)
            ->where('a.IID',$id);
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     */
    public static function getSecXj($id,$limit){
        $model = DB::table('circ_marc as a');
        $list = $model
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',5)
            ->where('a.IID',$id)
            ->groupBy('a.MarcID')
            ->select(DB::raw('a.Price,a.ImageUrl,a.Title,a.ReqNo,a.MarcID,count(b.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('circ_marc as a')
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',5)
            ->where('a.IID',$id);
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     */
    public static function getSecWs($id,$limit){
        $model = DB::table('circ_marc as a');
        $list = $model
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',6)
            ->where('a.IID',$id)
            ->groupBy('a.MarcID')
            ->select(DB::raw('a.Price,a.ImageUrl,a.Title,a.ReqNo,a.MarcID,count(b.SerialNo) as item_count'));
        $res['count'] = count($list->get());
        $model2 = DB::table('circ_marc as a')
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',6)
            ->where('a.IID',$id);
        $res['count2'] = $model2->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }
    /**
     */
    public static function getThirdWs($id,$limit){
        $model = DB::table('Items as a');
        $list = $model
            ->leftjoin('TagInfo as b','b.Barcode','=','a.BarCode')
            ->leftjoin('circ_marc as c','c.MarcID','=','a.MarcID')
            ->where('a.LTStatus',6)
            ->where('a.MarcID',$id)
            ->select('a.BarCode','b.rUID','a.sold_price','a.rent_price','a.SerialNo','c.IID');
        $res['count'] = $list->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }
    /**
     */
    public static function getThirdXj($id,$limit){
        $model = DB::table('Items as a');
        $list = $model
            ->leftjoin('TagInfo as b','b.Barcode','=','a.BarCode')
            ->leftjoin('circ_marc as c','c.MarcID','=','a.MarcID')
            ->where('a.LTStatus',5)
            ->where('a.MarcID',$id)
            ->select('a.BarCode','b.rUID','a.sold_price','a.rent_price','a.SerialNo','c.IID');
        $res['count'] = $list->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }
    /**
     */
    public static function getThirdCata($id,$limit){
        $model = DB::table('Items as a');
        $list = $model
            ->leftjoin('TagInfo as b','b.Barcode','=','a.BarCode')
            ->leftjoin('circ_marc as c','c.MarcID','=','a.MarcID')
            ->leftjoin('lendlist as l','l.BookID','=','a.SerialNo')
            ->leftjoin('doors as e','e.code','=','l.LendDoorID')
            ->leftjoin('shelves as f','f.id','=','e.sid')
            ->where('a.LTStatus',4)
            ->where('a.MarcID',$id)
            ->select(DB::raw('a.BarCode,b.rUID,a.sold_price,a.rent_price,a.SerialNo,a.LTStatus,c.IID,f.number,
            l.ReaderID,l.LendDate'));
        $res['count'] = $list->count();
        $res['list'] = $list->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     */
    public static function getThirdSj($id,$limit){
        $model = DB::table('Items as a')
            ->where('a.MarcID',$id)
            ->where('a.LTStatus',3);
        $list = $model
            ->leftjoin('TagInfo as b','b.Barcode','=','a.BarCode')
            ->leftjoin('circ_marc as c','c.MarcID','=','a.MarcID')
            ->leftjoin('booksindoor as d','d.BookEpc','=','b.rUID')
            ->leftjoin('doors as e','e.code','=','d.DoorID')
            ->leftjoin('shelves as f','f.id','=','e.sid')
            ->select(DB::raw('a.BarCode,b.rUID,a.sold_price,a.rent_price,a.SerialNo,a.LTStatus,c.IID,f.number'));
        $res['count'] = $list->count();
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    public static function getCollect($keyWord,$limit){
        $model = DB::table('ttindex7 as a');
        if (!empty($keyWord)) {
            $model->where("b.Title","like", '%'.$keyWord.'%')->orwhere('a.Content',"like", '%'.$keyWord.'%')->orwhere('b.ReqNo',"like", '%'.$keyWord.'%');
        }
        $list = $model
            ->leftjoin('circ_marc as b','b.IID','=','a.ID')
            ->leftjoin('Items as c','c.MarcID','=','b.MarcID')
            ->where('c.LTStatus',2)
            ->groupBy('a.ID')
            ->select('a.Content as isbn','b.ImageUrl','b.Title','b.ReqNo','a.ID');
        $res['count'] = count($list->get());
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     */
    public static function getSecCollect($id,$limit){
        $model = DB::table('circ_marc as a');
        $list = $model
            ->leftjoin('Items as b','a.MarcID','=','b.MarcID')
            ->where('b.LTStatus',2)
            ->where('a.IID',$id)
            ->groupBy('a.MarcID')
            ->select('a.Price','a.ImageUrl','a.Title','a.ReqNo','a.MarcID');
        $res = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    /**
     */
    public static function getThirdCollect($id,$limit){
        $model = DB::table('Items as a');
        $list = $model
            ->leftjoin('TagInfo as b','b.Barcode','=','a.BarCode')
            ->where('a.LTStatus',2)
            ->where('a.MarcID',$id)
            ->select('a.BarCode','b.rUID','a.sold_price','a.rent_price','a.SerialNo');
        $res = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }

    public static function  pushMessageToApp(){
        $host = config('config.ge_host');
        $appkey = config('config.ge_APPKEY');
        $mast = config('config.ge_MASTERSECRET');
        $appid = config('config.ge_APPID');
        $igt = new IGeTui($host,$appkey,$mast);
        //定义透传模板，设置透传内容，和收到消息是否立即启动启用
        $template = IGtNotificationTemplateDemo();
        $message = new IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $appIdList=array($appid);
        $message->set_appIdList($appIdList);
        $rep = $igt->pushMessageToApp($message,"任务组名");
        var_dump($rep);
    }

    public static function IGtNotificationTemplateDemo(){
        $appkey = config('config.ge_APPKEY');
        $appid = config('config.ge_APPID');
        $template =  new IGtNotificationTemplate();
        $template->set_appId($appid);                   //应用appid
        $template->set_appkey($appkey);                 //应用appkey
        $template->set_transmissionType(1);            //透传消息类型
        $template->set_transmissionContent("测试离线");//透传内容
        $template->set_title("个推");                  //通知栏标题
        $template->set_text("个推最新版点击下载");     //通知栏内容
        $template->set_logo("");                       //通知栏logo
        $template->set_logoURL("");                    //通知栏logo链接
        $template->set_isRing(true);                   //是否响铃
        $template->set_isVibrate(true);                //是否震动
        $template->set_isClearable(true);              //通知栏是否可清除

        return $template;
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = DB::table('Items as a')
            ->where('a.SerialNo',$id)
            ->leftJoin('ttindex7 as b','b.MarcID','=','a.MarcID')
            ->leftJoin('img as c','c.isbn','=','b.Content')
            ->select('c.img')
            ->first();
        return Result::success()->setData($res);
    }

    public static function getPic($id) {
        $res = DB::table('circ_marc as a')
            ->where('a.MarcID',$id)
            ->select('a.ImageUrl as img')
            ->first();
        return Result::success()->setData($res);
    }

    /**
     * 二级下拉
     * czs
     */
    public static function pull1($pid) {
        $res = DB::table('regions')->where('id',$pid)
            ->where('pid',0)
            ->select('id', 'name')->get();
        return Result::success()->setData($res);
    }

    /**
     * 二级下拉
     * czs
     */
    public static function pull($pid) {
        $res = DB::table('regions')->where('pid',$pid)->select('id', 'name')->get();
        return Result::success()->setData($res);
    }

    /**
     * wap城市下拉
     * czs
     */
    public static function cPull($pid) {
        if (!empty($pid)) {
            $res = SceneryRegion::where('pid',$pid)->where('status',1)->select('id', 'name')->get()->toarray();
        } else {
            $res = 0;
        }
        return Result::success()->setData($res);
    }

    /**
     * 三级下拉
     * czs
     */
    public static function pull2($rid) {
        $res = Scenery::where('region_id',$rid)->select('id', 'name')->get()->toarray();
        return Result::success()->setData($res);
    }


    /**
     *更新图片
     * czs
     */
    public static function updateImg($data) {
        $sel = DB::table('img')->where('isbn',$data['isbn']);
        if($sel->count()>0) {
            $sel->update(['img'=>$data['img']]);
        } else {
            DB::table('img')->insert($data);
        }
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        if (empty($input['Title'])) {
            return Result::failure()->setMsg("书名不能为空");
        }
        if (empty($input['Price'])||$input['Price']<=0) {
            return Result::failure()->setMsg("定价不能为空");
        }
        if (empty($input['Profile'])) {
            return Result::failure()->setMsg("简介不能为空");
        }
        $res = DB::table('circ_marc as a')
            ->where('a.MarcID',$id)
            ->update($input);
        return Result::success()->setData($res);
    }

    public static function updateThirdCata($input, $id) {
        if (empty($input['sold_price'])||$input['sold_price']<=0) {
            return Result::failure()->setMsg("售价不能为空");
        }
        if (empty($input['rent_price'])||$input['rent_price']<=0) {
            return Result::failure()->setMsg("租金不能为空");
        }
        $res = DB::table('Items as a')
            ->where('a.SerialNo',$id)
            ->update($input);
        return Result::success()->setData($res);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $sel = DB::table('Items as a')
            ->leftJoin('circ_marc as b','a.MarcID','=','b.MarcID')
            ->where('a.SerialNo',$id);
        if ($sel->count()) {
            $sel->delete();
        }
        return Result::success()->setData($sel);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteType($id) {
        $sel = DB::table('book_type')->where('id',$id);
        if ($sel->count()) {
            $sel->delete();
        }
        return Result::success()->setData($sel);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        Scenery::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * 景区下拉
     * czs
     */
    public static function sceneryPull() {
        $res = Scenery::select('id', 'name')->get()->toarray();
        return Result::success()->setData($res);
    }

    /**
     * 父类别下拉
     * czs
     */
    public static function parentList() {
        $res = DB::table('regions as a')
            ->where('a.pid',0)
            ->select('a.id', 'a.name')
            ->get();
        return Result::success()->setData($res);
    }

    /**
     * wap父景区
     * czs
     */
    public static function pPull($id) {
        $res = DB::table('scenery_regions as a')
            ->where('a.id',$id)
            ->pluck('a.name');
        return Result::success()->setData($res);
    }

    /**
     * wap子类别
     * czs
     */
    public static function childPull($id) {
        $res = DB::table('scenery_regions as a')
            ->where('a.id',$id)
            ->pluck('a.name');
        return Result::success()->setData($res);
    }

    /**
     * 联动下拉
     * czs
     */
    public static function contactList($pid) {
        $res = SceneryRegion::where('pid',$pid)->select('id', 'name')->get();
        return Result::success()->setData($res);
    }

    /**
     * 前台景区列表
     * czs
     */
    public static function webSceneryList($rid,$type,$key,$grade,$limit) {
        $model = DB::table('sceneries as a')->where('a.status',1);
        if($type){
            $model->where('a.type_id',$type);
        }
        if($key){
            $model->where('a.name','like','%'.$key.'%');
        }
        if($grade){
            $model->where('a.grade',$grade);
        }
        if($rid){
            $model->whereRaw('(ypw_a.region_id ='.$rid.' or ypw_b.pid = '.$rid.')');
        }
        $model->leftJoin('scenery_regions as b','b.id','=','a.region_id')
            ->leftJoin('pictures as c','c.id','=','a.cover_id')
            ->orderBy('a.grade','desc')
            ->orderBy('a.sort','desc')
            ->orderBy('a.id','desc');
        $list = $model->select('a.*','b.name as bname','c.img')->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     * 前台景区列表
     * czs
     */
    public static function sceneryList($pid,$rid,$type,$key,$limit) {
        $model = DB::table('sceneries as a')->where('a.status',1);
        if($type){
            $model->where('a.type_id',$type);
        }
        if($key){
            $model->where('a.name','like','%'.$key.'%');
        }
        if($rid){
            $model->where('a.region_id',$rid);
        } elseif($pid){
            $model->where('b.pid',$pid);
        }

        $model->leftJoin('scenery_regions as b','b.id','=','a.region_id')
            ->leftJoin('pictures as c','c.id','=','a.cover_id')
            ->orderBy('a.grade','desc')
            ->orderBy('a.sort','desc')
            ->orderBy('a.id','desc');
        $list = $model->select('a.*','b.name as bname','c.img')->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     *前台景区详情
     * czs
     */
    public static function sceneryOne($id) {
        $model = DB::table('sceneries');
        $res = $model->where('id',$id)->where('status',1)->first();
        return Result::success()->setData($res);
    }

    /**
     *wap景区详情
     * czs
     */
    public static function sceneryDetail($id) {
        $model = DB::table('sceneries as a');
        $res = $model->where('a.id',$id)
            ->leftJoin('function_details as b','b.id','=','a.detail_id')
            ->select('a.*','b.type','b.function_id','a.pic_ids')
            ->first();
        return Result::success()->setData($res);
    }

    /**
     * 根据景区找到商品
     * czs
     */
    public static function findGoods($sceneryId) {
        $model = DB::table('sceneries as a')->where('a.id',$sceneryId);
        $goodsId = $model->leftJoin('function_details as b','b.id','=','a.detail_id')
            ->leftJoin('products as c','c.function_id','=','b.function_id')
            ->where('category_id',2)
            ->pluck('c.id');
        return Result::success()->setData($goodsId);
    }

    /**
     * ltLog
     */
    public static function getLtlogs($keyWord,$limit){
        //先取异常书
        $model=Lendhist::leftjoin('Items as c','c.SerialNo','=','lendhist.BookID')
            ->leftJoin('TagInfo as d','d.Barcode','=','c.BarCode')
            ->leftjoin('circ_marc as b','b.MarcID','=','c.MarcID')
            ->leftjoin('ttindex7 as a','b.IID','=','a.ID');

        $status=['外借'=>0,'还书'=>1,'已售'=>2];

        if (!empty($keyWord)) {
            $model->where(function ($query) use ($keyWord,$status) {
                $query->where("b.Title","like", '%'.$keyWord.'%')
                    ->orwhere('a.Content',"like", '%'.$keyWord.'%')
                    ->orwhere('b.ReqNo',"like", '%'.$keyWord.'%')
                    ->orwhere('c.BarCode',"like", '%'.$keyWord.'%')
                    ->orwhere("a.Title","like", '%'.$keyWord.'%')
                    ->orwhere("lendhist.ReaderID","like", '%'.$keyWord.'%');
                if(isset($status[$keyWord])){
                    $query->orwhere("lendhist.BookState","=", "{$status[$keyWord]}");
                }
            });
        }
        $list = $model
            ->select('a.Content as isbn','b.ImageUrl','a.Title','b.ReqNo','a.ID','b.Title as cTitle','c.SerialNo','c.Barcode','lendhist.ReaderID','lendhist.LendDoorID','lendhist.LendDate','lendhist.ReturnDate','lendhist.ReturnDoorID','lendhist.BookState','d.rUID');
        $res['count'] = count($list->get());
        $res['list'] = $list
            ->paginate($limit);
        return Result::success()->setData($res);
    }
}


